package com.mx.tool.form.window.encryption;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.IdUtil;
import com.mx.tool.enums.EncryptionEnum;
import com.mx.tool.helper.IEncryp;
import com.mx.tool.util.NotifyUtil;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * @Author: 徐建鹏.
 * @create: 2021-11-10 08:53
 * @Description:
 */
public class EncryptionWindow {

    private JPanel jPanel;
    private JTextArea contentArea;
    private JTextArea showArea;
    private JComboBox<String> wayBox;
    private JTextField passText;
    private JButton jamButton;
    private JButton jemButton;
    private JButton createKeyButton;
    private JButton clearButton;
    private JButton hhButton;
    private byte[] keys;
    private String password;
    private final Map<String, IEncryp> encrypMap = IEncryp.init();


    public EncryptionWindow() {
        jamButton.addActionListener(e -> jam());
        jemButton.addActionListener(e -> jem());
        createKeyButton.addActionListener(e -> passText.setText(IdUtil.fastSimpleUUID()));
        clearButton.addActionListener(e -> {
            contentArea.setText("");
            showArea.setText("");
        });
        hhButton.addActionListener(e -> {
            String content = contentArea.getText();
            String show = showArea.getText();
            contentArea.setText(show);
            showArea.setText(content);
        });
    }

    /**
     * 加密
     */
    private void jam() {
        String content = contentArea.getText();
        String wayStr = Convert.toStr(wayBox.getSelectedItem());
        IEncryp encryp = encrypMap.get(wayStr);
        if (encryp == null) {
            NotifyUtil.error("请选择正确的加密方式");
            return;
        }
        String result;
        if(wayStr.equals(EncryptionEnum.RC4.value())){
            String key = StringUtils.isNotBlank(passText.getText()) ? passText.getText() : "";
            result = encryp.encrypt(content, key);
            password = encryp.getKey();
        }else{
            byte[] key = StringUtils.isNotBlank(passText.getText()) ? passText.getText().getBytes(StandardCharsets.UTF_8) : new byte[]{};
            result = encryp.encrypt(content, key);
            keys = encryp.getKeys();
        }

        showArea.setText(result);
    }

    /**
     * 解密
     */
    private void jem() {
        showArea.setText("");
        String content = contentArea.getText();

        String wayStr = Convert.toStr(wayBox.getSelectedItem());
        IEncryp encryp = encrypMap.get(wayStr);
        if (encryp == null) {
            NotifyUtil.error("请选择正确的解密方式");
            return;
        }
        String result;
        if(wayStr.equals(EncryptionEnum.RC4.value())){
            String key = StringUtils.isNotBlank(passText.getText()) ? passText.getText() : password;
            result = encryp.decrypt(content, key);
        }else{
            byte[] key = StringUtils.isNotBlank(passText.getText()) ? passText.getText().getBytes(StandardCharsets.UTF_8) : keys;
             result = encryp.decrypt(content, key);
        }

        showArea.setText(result);
    }

    public JPanel getContent() {
        return jPanel;
    }
}
